import axios from 'axios'
import { debug } from '@/model'
import { getAuth } from '@/web3'

const http = axios.create({
  baseURL: import.meta.env.VITE_API_URL,
  timeout: 10000
});

// 请求拦截
http.interceptors.request.use(config => {
  // 传递钱包
  config.headers['Authorization'] = getAuth()

  return config;
});

// 响应拦截
http.interceptors.response.use(response => {
  const data = response.data

  // 检查是否成功
  if(data.type == 'success') {
    return data.data
  }
  
  // 如果是维护
  if(data.code == 50002) {
    debug(1)
  }
  
  // 未授权
  if(data.code == 65002) {
    debug(2)
  }
  
  // 如果拉黑
  if(data.code == 65003) {
    debug(3)
  }
  
  // 需要注册
  if(data.code == 65006) {
    debug(4)
  }

  return Promise.reject(data);
}, () => {
  return Promise.reject()
});

export default http